package com.hg.library.widget;

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Path;
import android.graphics.PorterDuff;
import android.graphics.PorterDuffXfermode;
import android.util.AttributeSet;
import android.view.View;
import android.widget.Button;

/**
 * Created by admin on 2017/2/24.
 */

public class CuteButton extends Button {
    public CuteButton(Context context, AttributeSet attrs) {
        super(context, attrs);
        setBackgroundColor(getResources().getColor(android.R.color.transparent));
    }

    private boolean isStart;
    volatile
    Paint mPaint = new Paint();

    float mRotate = 0;
    int count;

    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);
        canvas.drawARGB(0, 255, 255, 255);

        float x = (getWidth() - 7) / 2 + 7;
        float y = (getHeight() - 7) / 2 + 7;


        if (isStart) {

            if (count == 4){
                isStart = false;
                mRotate = 0;
                count = 0;
            }else if (count % 2 == 0) {
                drawP1();
            } else if (count % 2 == 1) {
                drawP2();
            }
        }

        canvas.rotate(-mRotate, x - 1, y - 3);

        {
            int saveLayerId = canvas.saveLayer(0, 0, getWidth(), getHeight() - 15, null, Canvas.ALL_SAVE_FLAG);
            mPaint.setStyle(Paint.Style.STROKE);
            mPaint.setAntiAlias(true);
            mPaint.setStrokeWidth(5);
            mPaint.setColor(Color.argb(255, 65, 72, 75));

            canvas.clipRect(0, 0, getWidth(), getHeight() - 15);

            Path path = new Path();
            path.addCircle(20, 20 + 3, 7, Path.Direction.CCW);

            mPaint.setStrokeWidth(6);
            canvas.drawLine(25, 25 + 3, getWidth(), getHeight() - 5, mPaint);
            canvas.drawPath(path, mPaint);

            mPaint.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.CLEAR));
            canvas.drawRect(getWidth() - 10 - 10, getHeight() - 15, getWidth(), getHeight() + 10, mPaint);
            drawPoint(canvas);

            canvas.restoreToCount(saveLayerId);
        }
        canvas.rotate(mRotate, x - 1, y - 3);
        canvas.rotate(mRotate, x - 1, y - 3);
        {
            int saveLayerId = canvas.saveLayer(0, 15, getWidth(), getHeight(), null, Canvas.ALL_SAVE_FLAG);

            mPaint.setStrokeWidth(5);
            canvas.drawCircle(20, getHeight() - 20 - 3, 7, mPaint);

            mPaint.setStrokeWidth(6);
            canvas.drawLine(25, getHeight() - 25 - 3, getWidth(), 0 + 5, mPaint);

            mPaint.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.CLEAR));
            canvas.drawRect(0, 0, getWidth(), 15, mPaint);
            drawPoint(canvas);

            mPaint.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.CLEAR));
            mPaint.setStrokeWidth(4);
            canvas.drawLine(31, 25 + 3+mRotate/2, getWidth(), getHeight() - 12-mRotate*1.5f,mPaint);
            mPaint.setXfermode(null);

            canvas.restoreToCount(saveLayerId);
        }
        canvas.rotate(-mRotate, x - 1, y - 3);
    }

    private void drawPoint(Canvas canvas) {
        mPaint.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.CLEAR));
        mPaint.setStrokeWidth(2);
        float x = (getWidth() - 7) / 2 + 7;
        float y = (getHeight() - 7) / 2 + 7;
        canvas.drawPoint(x - 1, y - 3, mPaint);

        mPaint.setXfermode(null);
    }

    private void drawP1() {
        if (isStart) {
            mRotate += 1.5f;
            if (mRotate >= 15) {
                count++;
            }
            invalidate();
        }

    }

    private void drawP2() {
        if (isStart) {
            mRotate -= 1.5f;
            if (mRotate <= 0) {
                count++;
            }
            invalidate();
        }
    }

    public void click() {
        if (!isStart) {
            isStart = true;
            invalidate();
        }
    }
}
